package com.open.pokergames.comm.userCenter

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.CountDownTimer
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.gyf.immersionbar.ImmersionBar
import com.open.pokergames.MainActivity
import com.open.pokergames.MainApplication
import com.open.pokergames.NetConfig
import com.open.pokergames.R
import com.open.pokergames.comm.repository.cache.LocalCache
import com.sinothk.android.utils.XUtils
import com.sinothk.permission.PermissionManager
import com.sinothk.permission.PermissionResultListener
import com.sinothk.permission.lib.PermissionsUtil

class WelcomeActivity : AppCompatActivity() {

    private fun checkPermissions() {
        val tip = PermissionsUtil.TipInfo("授权提示", "获取必须的手机权限不全！", "放弃使用", "授权")
        if (PermissionManager.haveAllPermission(this, MainApplication.permissions)) {
            timer.start()
        } else {
            PermissionManager.requestAllPermission(this, object : PermissionResultListener {
                override fun permissionFail(p0: Array<out String>?) {
                    XUtils.toast().show("放弃使用")
                    finish()
                }

                override fun permissionSuccess(p0: Array<out String>?) {
                    checkPermissions()
                }
            }, MainApplication.permissions, true, tip)
        }
    }

    private fun initNoticeTitle() {
        // 标题处理
        ImmersionBar.with(this)
            .statusBarDarkFont(false)
            .keyboardEnable(false)
            .keyboardMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
            .init()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)
        checkPermissions()
        initNoticeTitle()
        //获取唤醒参数
//        XInstall.getWakeUpParam(getIntent(), wakeUpAdapter);
    }

//    override fun onNewIntent(intent: Intent?) {
//        super.onNewIntent(intent)
//        // 此处要调用，否则App在后台运行时，会无法截获
//        XInstall.getWakeUpParam(getIntent(), wakeUpAdapter)
//    }

//    var wakeUpAdapter: XWakeUpAdapter = object : XWakeUpAdapter() {
//        override fun onWakeUp(XAppData: XAppData) {
//            //获取渠道数据
//            val channelCode = XAppData.channelCode
//
//            //获取数据
//            val data = XAppData.extraData
//            //通过链接后面携带的参数或者通过webSdk初始化传入的data值。
//            val uo = data["uo"]
//            //webSdk初始，在buttonId里面定义的按钮点击携带数据
//            val co = data["co"]
//
//            //获取时间戳
//            val timeSpan = XAppData.timeSpan
//        }
//    }

    override fun onDestroy() {
        super.onDestroy()
//        wakeUpAdapter = null
    }

    @SuppressLint("SetTextI18n")
    var timer: CountDownTimer = object : CountDownTimer(2000, 1000) {

        override fun onTick(millisUntilFinished: Long) {
//            timerTv.text = "${millisUntilFinished / 1000}s"
        }

        override fun onFinish() {
            if (LocalCache.getAutoLogin()) {
                if (NetConfig.getHeader() == null) {
                    toLogin()
                } else {
                    XUtils.intent().openActivity(this@WelcomeActivity, MainActivity::class.java)
                        .finish(true).start()
                }
            } else {
                toLogin()
            }
        }
    }

    private fun toLogin() {
        XUtils.intent().openActivity(this@WelcomeActivity, LoginActivity::class.java)
            .finish(true).start()
    }
}
